package com.zkadmin.admin.repository;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.zkadmin.entity.SysMenu;
import com.zkadmin.entity.SysResource;
import com.zkadmin.entity.SysRole;
import com.zkadmin.entity.SysUser;
import com.zkadmin.service.SysMenuService;
import com.zkadmin.service.SysResourceService;
import com.zkadmin.service.SysRoleService;
import com.zkadmin.service.SysUserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-beans.xml" })
@TransactionConfiguration(defaultRollback = false)
public class InitZkadmin {

	@Autowired
	protected SysUserService userService;

	@Autowired
	protected SysResourceService resourceService;

	@Autowired
	protected SysRoleService roleService;

	@Autowired
	protected SysMenuService menuService;

	@Test
	@Transactional
	public void initData() {
		List<SysMenu> menus = new ArrayList<>();
		menus.add(new SysMenu("系统管理", "10", null, "z-icon-cogs", 0));
		menus.add(new SysMenu("菜单管理", "1001", "admin/menu_list.zul", "z-icon-file-text-o", 0));
		menus.add(new SysMenu("用户管理", "1002", "admin/user_list.zul", "z-icon-user-md", 0));
		menus.add(new SysMenu("角色管理", "1003", "admin/role_list.zul", "z-icon-user", 0));
		menus.add(new SysMenu("资源管理", "1004", "admin/resource_list.zul", "z-icon-list-alt", 0));
		menus.add(new SysMenu("系统配置", "1098", "admin/setting.zul", "z-icon-file-text-o", 0));
		menus.add(new SysMenu("系统日志", "1099", "admin/log_list.zul", "z-icon-file-text-o", 0));

		List<SysResource> resources = new ArrayList<>();
		resources.add(new SysResource("系统菜单管理", "10", "sys:menu:*"));
		resources.add(new SysResource("系统菜单新建", "1001", "sys:menu:create"));
		resources.add(new SysResource("系统菜单修改", "1002", "sys:menu:update"));
		resources.add(new SysResource("系统菜单删除", "1003", "sys:menu:delete"));
		resources.add(new SysResource("系统用户管理", "11", "sys:user:*"));
		resources.add(new SysResource("系统用户新建", "1101", "sys:user:create"));
		resources.add(new SysResource("系统用户修改", "1102", "sys:user:update"));
		resources.add(new SysResource("系统用户删除", "1103", "sys:user:delete"));
		resources.add(new SysResource("分配用户角色", "1104", "sys:user:role"));
		resources.add(new SysResource("重置用户密码", "1105", "sys:user:reset_pwd"));
		resources.add(new SysResource("系统角色管理", "12", "sys:role:*"));
		resources.add(new SysResource("系统角色新建", "1201", "sys:role:create"));
		resources.add(new SysResource("系统角色修改", "1202", "sys:role:update"));
		resources.add(new SysResource("系统角色删除", "1203", "sys:role:delete"));
		resources.add(new SysResource("角色菜单授权", "1204", "sys:role:menu"));
		resources.add(new SysResource("角色资源授权", "1205", "sys:role:resource"));
		resources.add(new SysResource("系统资源管理", "13", "sys:resource:*"));
		resources.add(new SysResource("系统资源新建", "1301", "sys:resource:create"));
		resources.add(new SysResource("系统资源修改", "1302", "sys:resource:update"));
		resources.add(new SysResource("系统资源删除", "1303", "sys:resource:delete"));

		SysRole role = new SysRole("role_admin", "超级管理员");
		for (SysMenu sysMenu : menus) {
			role.addMenu(sysMenu);
			menuService.save(sysMenu);
		}

		for (SysResource sysResource : resources) {
			role.addResource(sysResource);
			resourceService.save(sysResource);
		}
		roleService.save(role);

		SysUser user = new SysUser();
		user.setName("超级管理员");
		user.setUsername("admin");
		user.setPassword("admin");
		user.addRole(role);
		userService.save(user);
	}
}
